package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.DAOSExceptions;
import es.ats.codington.festival.services.VisitorService;


@WebServlet("/LoginServlets")
public class LoginServlets extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final boolean Admin = false;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlets() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");  
	
	    String n=request.getParameter("username");  
	    String p=request.getParameter("password");
		    
	    Visitor u=new Visitor();
	    u.setUsername(n);
	    u.setPassword(p);
	    VisitorService vs=new VisitorService();
	    
	    try{
		    int idvisitor =vs.loginVisitor(u);
		    
		    HttpSession session = request.getSession(true);
		    
		    if (idvisitor ==0){	
		       	RequestDispatcher rd2=request.getRequestDispatcher("mainpage.jsp");  
		         rd2.forward(request,response);  
		      
		    		    		    	
		    }else{
		    	if(idvisitor==1){
		    	  Integer iD = idvisitor;
				    session.setAttribute("iDVisitor", iD);
				    session.setAttribute("username", n);
				    session.setAttribute("password",p);
				    RequestDispatcher rd=request.getRequestDispatcher("mainpageadmin.jsp");  
				    rd.forward(request,response);
				   
		    	}else{
		    		Integer iD = idvisitor;
				    session.setAttribute("iDVisitor", iD);
				    session.setAttribute("username", n);
				    session.setAttribute("password",p);
		    		RequestDispatcher rd=request.getRequestDispatcher("mainpage2.jsp");  
				    rd.forward(request,response);
		    	}
		    }
	        
		}catch(Exception ex){
			String msg= ex.getMessage();
			request.setAttribute("msg",msg); 
			RequestDispatcher dispatcher = request.getRequestDispatcher("mainpage.jsp");
            dispatcher.forward(request, response);
		
		}
	
}
}
